<?php

namespace app\model;

use think\facade\Db;
use think\Model;

class BookContentsModel extends Model {

    protected $table ="book_contents";

    protected $pk = 'content_id';

    private $cacheKey="chapter:content:id";

    private $cacheTime=5184000;


    public function getChapterInfo($chapter_id){
        $cacheKey = $this->cacheKey.$chapter_id;
        $redisModel = new RedisModel();
        $info = $redisModel->get($cacheKey);
        if($info){
            return json_decode($info,true);
        }

        $info = Db::name($this->table)->where("chapter_id",$chapter_id)->find();

        if($info){
            $redisModel->set($cacheKey,json_encode($info,256),$this->cacheTime);
            return $info;
        }
        return $info;
    }






    public function addOne($data){
        return Db::name($this->table)->insertGetId($data);
    }



    public function updateAll($where,$data){
        return Db::name($this->table)->where($where)->update($data);
    }


    public function delAll($where){
        return Db::name($this->table)->where($where)->delete();
    }



}